##000000000000000000000000000000000000 Setup the project
cmake_minimum_required(VERSION 3.16...3.27)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
    cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()

project(g4test LANGUAGES CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 17)
set(myEXE ${PROJECT_NAME})

##000000000000000000000000000000000000 添加 G4 搜索路径
if(WIN32)
    add_compile_definitions(WIN32)
    add_compile_options(/utf-8)
    list(PREPEND CMAKE_PREFIX_PATH "C:/cpplibs/geant4-11.3.2/lib/cmake/Geant4")
elseif(LINUX)
    if(CMAKE_BUILD_TYPE STREQUAL "Debug") # geant4库 debug版路径
        list(PREPEND CMAKE_PREFIX_PATH "$ENV{HOME}/cpplibs/geant4v11.3.2debug")
    else() # geant4库 release 版路径
        list(PREPEND CMAKE_PREFIX_PATH "$ENV{HOME}/cpplibs/geant4v11.3.2/lib64/cmake/Geant4")
        ## 兼容 ubuntu 路径
        list(PREPEND CMAKE_PREFIX_PATH "$ENV{HOME}/cpplibs/geant4v11.3.2/lib/cmake/Geant4")
    endif()
    ## 强制使用你指定的库路径
    #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--disable-new-dtags -Wl,-rpath,/home/tom/cpplibs/geant4v11.3.2/lib64 -Wl,--no-as-needed")
endif(WIN32)


##000000000000000000000000000000000000 conan2 兼容设置
# conan2 的 expat 包的 target 是小写的 expat::expat;
# geant4 期待大写的 EXPAT::EXPAT
if(LINUX)
    find_package(expat REQUIRED)
    add_library(EXPAT::EXPAT ALIAS expat::expat)
endif()

find_package(fmt REQUIRED)
find_package(spdlog CONFIG REQUIRED)
find_package(cnpy CONFIG REQUIRED)

##000000000000000000000000000000000000
# Find Geant4 package, activating all available UI and Vis drivers by default
# You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui
# to build a batch mode only executable
option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
if(WITH_GEANT4_UIVIS)
    find_package(Geant4 REQUIRED ui_all vis_all)
else()
    find_package(Geant4 REQUIRED)
endif()
##000000000000000000000000000000000000
# Setup Geant4 include directories and compile definitions
include(${Geant4_USE_FILE})
message(DEBUG "[EXPAT] Geant4_builtin_expat_FOUND = ${Geant4_builtin_expat_FOUND}")
# message(STATUS "[Geant4] Geant4_LIBRARIES = ${Geant4_LIBRARIES}")
# message(STATUS "[Geant4] Geant4_INCLUDE_DIR = ${Geant4_INCLUDE_DIR}")
message(STATUS "[Geant4] Geant4_USE_FILE = ${Geant4_USE_FILE}")

##000000000000000000000000000000000000 Locate sources and headers for this project
include_directories(
    "${PROJECT_SOURCE_DIR}/include"
    "${Geant4_INCLUDE_DIR}"
)
##00000000000000000000000000000000000000000000 头文件列表
set(headers
    include/ActionInitialization.hh
    include/args.hxx
    include/CliArgs.hh
    include/DetectorConstruction.hh
    include/DetectorMessenger.hh

    include/PhysicsList.hh
    include/PhysicsListMessenger.hh
    include/PrimaryGeneratorAction.hh
    include/PrimaryGeneratorMessenger.hh

    include/RunAction.hh
    include/RunData.hh

    include/Util.hh
    include/G4Util.hh

    include/MyConstant.hh
    include/MyMacros.hh
    include/MyInitial.hh

    include/LETdMessenger.hh
    include/PSSDoseAveragedLET.hh

    include/RgProjScorer.hh
    include/RgProjScorerMsgr.hh

    include/AtomNumFilter.hh
    include/AtomNumFilterMsgr.hh

    include/MyNpzScoreWriter.hh
)
##00000000000000000000000000000000000000000000 源文件列表
set(sources
    src/ActionInitialization.cc
    src/DetectorConstruction.cc
    src/DetectorMessenger.cc
    src/PhysicsList.cc
    src/PhysicsListMessenger.cc
    src/PrimaryGeneratorAction.cc
    src/PrimaryGeneratorMessenger.cc

    src/RunAction.cc
    src/RunData.cc

    src/G4Util.cc

    src/LETdMessenger.cc
    src/PSSDoseAveragedLET.cc

    src/RgProjScorer.cc
    src/RgProjScorerMsgr.cc

    src/AtomNumFilter.cc
    src/AtomNumFilterMsgr.cc

    src/MyNpzScoreWriter.cc
)

##000000000000000000000000000000000000
# Add the executable, and link it to the Geant4 libraries
add_executable(${myEXE} g4test.cc ${sources} ${headers})
target_link_libraries(${myEXE}
  PRIVATE
    ${Geant4_LIBRARIES}
    fmt::fmt
    spdlog::spdlog $<$<BOOL:${MINGW}>:ws2_32>
    cnpy::cnpy
)

## 000000000000000000000000000000000000
# Install the executable to 'bin' directory under CMAKE_INSTALL_PREFIX
install(TARGETS ${myEXE} DESTINATION bin)


##000000000000000000000000000000000000
## 将所有 scripts 拷贝到 build 目录. exe 运行的时候，会从所在目录寻找 scripts.
set(EXAMPLEB1_SCRIPTS
    init_vis.mac
    vis.mac
    letd.mac
    qa1.mac
)

foreach(_script ${EXAMPLEB1_SCRIPTS})
    configure_file(
        ${PROJECT_SOURCE_DIR}/${_script}
        ${PROJECT_BINARY_DIR}/${_script}
        COPYONLY
    )
endforeach()
